package com.framework.modules.job.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.flow.FlowTask;
import com.framework.entity.sys.SysAgentInfo;
import com.framework.entity.sys.SysBank;
import com.framework.entity.sys.SysPayment;
import com.framework.service.AgentInfoService;
import com.framework.service.BankService;
import com.framework.service.FlowTaskService;
import com.framework.service.PaymentService;
import com.framework.utils.jiufu.StringUtils;
import com.framework.utils.pay.yunzhanghujiesuan.YunJieSuanPay;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author wq
 * @date 2019/10/28 16:31
 * @description
 */
public class BaiyiCommitTakeTask {

    @Autowired
    private FlowTaskService flowTaskService;

    @Autowired
    private BankService bankService;

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private AgentInfoService agentInfoService;


    public void commitTaks() {
        List<FlowTask> tasks = flowTaskService.selectList(new EntityWrapper<FlowTask>().eq("task_state", "wait"));

        for (FlowTask task : tasks) {
            synchronized (task) {
                Long agentId = task.getAgentId();
                SysBank bank = bankService.queryBankByAgentId(agentId);
                if (bank == null) {
                    task.setTaskState("failure");
                    task.setExplains("提现失败--未查询到银行卡");
                    flowTaskService.updateById(task);
                    continue;
                }
                SysAgentInfo infoById = agentInfoService.getAgentInfoById(agentId);

                if (infoById == null) {
                    task.setTaskState("failure");
                    task.setExplains("提现失败--未查询到详细信息");
                    flowTaskService.updateById(task);
                    continue;
                }

                if (StringUtils.isNull(infoById.getIdCard())) {
                    task.setTaskState("failure");
                    task.setExplains("提现失败--未查询到身份证信息");
                    flowTaskService.updateById(task);
                    continue;
                }

                SysPayment sysPayment = paymentService.selectOne(new EntityWrapper<SysPayment>().eq("dept_id", bank.getDeptId()).eq("take_type", task.getTakeType()));
                if (sysPayment == null) {
                    task.setTaskState("failure");
                    task.setExplains("提现失败--未查询到代付配置信息");
                    flowTaskService.updateById(task);
                    continue;
                }

                YunJieSuanPay.yunJieSuanPay(task.getTakeNo(), bank, infoById.getIdCard(), String.valueOf(task.getActAccountAmount()), sysPayment, sysPayment.getPayName());

                task.setTaskState("EXAMINE");
                task.setExplains("提现审核中");
                flowTaskService.updateById(task);

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }


}
